//
//  HealthPayController.m
//  village_ios
//
//  Created by mac on 2019/8/9.
//  Copyright © 2019 com.sw.village. All rights reserved.
//

#import "HealthPointController.h"
#import "PointModel.h"


@interface PointListCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *pointLabel;
@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;
@property (weak, nonatomic) IBOutlet UILabel *executorLabel;
@property (weak, nonatomic) IBOutlet UILabel *executeDateLabel;

@property (strong, nonatomic) PointModel *point;
@end

@implementation PointListCell

- (void)setPoint:(PointModel *)point {
    _point = point;
    
    if (point==nil) {
        self.dateLabel.text = @"扣分时间";
        self.pointLabel.text = @"分值";
        self.reasonLabel.text = @"原因";
        self.executorLabel.text = @"执行人";
        self.executeDateLabel.text = @"执行时间";
    }else {
        self.dateLabel.text = point.createTime;
        self.pointLabel.text = [NSString stringWithFormat:@"%@分",@(point.point)];
        self.reasonLabel.text = point.detail;
        self.executorLabel.text = point.executor;
        self.executeDateLabel.text = point.createTime;
    }
    
   
}

@end

@interface HealthPointController ()
//data
@property (strong, nonatomic) NSMutableArray *dataList;
@property (assign, nonatomic) NSInteger page;
@end

@implementation HealthPointController

- (void)viewDidLoad {
    [super viewDidLoad];
    //init
    self.title = self.menuItem.title;
    self.page = 1;
    self.dataList = [NSMutableArray array];
    //header
    [self setupHeader];
    //network
    [self loadNetworkData];
}
- (void)setupHeader {
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        self.page = 1;
        [weakSelf loadNetworkData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page == 1) {
            self.page++;
        }
        [weakSelf loadNetworkData];
    }];
}
- (void)loadNetworkData {

    [SYProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if(self.page) {
        [params addEntriesFromDictionary:@{@"page":@(self.page),@"size":@(20)}];
    }
    NSDictionary *menuParam = [StringUtils dictionaryWithJsonString:self.menuItem.param];
    if(menuParam!=nil) {
        [params addEntriesFromDictionary:menuParam];

    }

    [APIManager requestWithUrlString:@"api/point/list" params:params method:@"GET" success:^(id response) {
        if(self.page == 1) {
            self.dataList = [PointModel mj_objectArrayWithKeyValuesArray:response];
        } else {
            NSArray *list = [PointModel mj_objectArrayWithKeyValuesArray:response];
            [self.dataList addObjectsFromArray:list];
        }
        [self reloadAllData];
    } failure:^(NSError *error) {
        [self reloadErrorData];
    }];


}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    PointListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PointListCell"];
    if (indexPath.row==1) {
        cell.point = nil;
    }else {
        PointModel *point =  self.dataList[indexPath.row];
        cell.point = point;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}


@end
